package com.leeh.jsontest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Liheng on 2017-04-17.
 */

public class CourseViewOne extends LinearLayout {

    private TextView txtTitle;
    private TextView txtTerm;
    private TextView txtCourseBaseInfo;
    private List<View> includeViewList;
    private List<TextView> txtCourseList;
    private OnClickListener clickListener = null;
    private int []textViewStyle = { R.drawable.style_textview1, R.drawable.style_textview2, R.drawable.style_textview3,
            R.drawable.style_textview4, R.drawable.style_textview5, R.drawable.style_textview6,
            R.drawable.style_textview7,};

    public CourseViewOne(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.schedule_course_type0, this, true);
        txtTitle = (TextView) findViewById(R.id.title_textView);
        txtTerm = (TextView) findViewById(R.id.term_texView);
        txtCourseBaseInfo = (TextView) findViewById(R.id.course_baseInfo_textView);

        includeViewList = new ArrayList<View>();
        txtCourseList = new ArrayList<TextView>();

        includeViewList.add(findViewById(R.id.course_content_one));
        includeViewList.add(findViewById(R.id.course_content_two));
        includeViewList.add(findViewById(R.id.course_content_three));
        includeViewList.add(findViewById(R.id.course_content_four));

        getTextViewFromIncludeView();

    }

    /**
     * 设置此组合控件的数据
     *
     * @param courseinfos
     */
    public void setData(List<String> courseinfos) {
        if (courseinfos.size() == 0) {
            for (int i = 0; i < txtCourseList.size(); i++) {
                txtCourseList.get(i).setBackgroundResource(R.drawable.textview_style_nothing_course);
                txtCourseList.get(i).setText("");
            }
            txtTitle.setText("没有课程");
            txtTerm.setText("");
            txtCourseBaseInfo.setText("");
            return;
        }
        //样式设置
        Random r = new Random();
        for (int i = 0; i < txtCourseList.size(); i++) {
            txtCourseList.get(i).setBackgroundResource(textViewStyle[r.nextInt(7)]);
        }
        txtTitle.setText(courseinfos.get(0));
        txtTerm.setText(courseinfos.get(1));
        txtCourseBaseInfo.setText(courseinfos.get(2));
        int[] startLocation = {13, 21, 30, 38};
        for (int i = 0; i < txtCourseList.size(); i++) {
            String content = courseinfos.get(startLocation[i / 7] + i % 7);
            if (content.equals("")) {
                txtCourseList.get(i).setBackgroundResource(R.drawable.textview_style_nothing_course);
            }
            txtCourseList.get(i).setText(content);
        }
    }

    /**
     * 从include里面拿到所有TextView
     */
    private void getTextViewFromIncludeView() {
        for (View view : includeViewList) {
            txtCourseList.add((TextView) view.findViewById(R.id.course_name1));
            txtCourseList.add((TextView) view.findViewById(R.id.course_name2));
            txtCourseList.add((TextView) view.findViewById(R.id.course_name3));
            txtCourseList.add((TextView) view.findViewById(R.id.course_name4));
            txtCourseList.add((TextView) view.findViewById(R.id.course_name5));
            txtCourseList.add((TextView) view.findViewById(R.id.course_name6));
            txtCourseList.add((TextView) view.findViewById(R.id.course_name7));
        }
    }

    /**
     * 设置监听事件
     *
     * @param clickListener
     */
    public void setOnClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
        //设置监听事件
        for (int i = 0; i < txtCourseList.size(); i++) {
            txtCourseList.get(i).setOnClickListener(this.clickListener);
        }
    }


}